package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class BoYaTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	private static Logger log = Logger.getLogger(BoYaTask.class);
	
//	@Scheduled(fixedDelay = 60000)
	public void queryBoYaOrder(){
		Map<String,Object> map = new HashMap<String,Object>();

		int flag = -1;
		try{
			String id = "0000000067";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);//获取通道的数据
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String queryOrderUrl = paramMap.get("queryOrderUrl")+"";//查询地址
			String key = paramMap.get("key");
			String appId = paramMap.get("appId");
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(Map<String,Object> order : orderList){
				String providerOrderId = order.get("providerOrderId")+"";//上家订单号
				String orderId = order.get("orderId")+"";
				map.put("orderId", orderId);
				String paramStr = "appId="+appId+"&sysOrderId="+providerOrderId;
				paramStr = paramStr + "&sign=" + MD5.ToMD5(paramStr + "&secretKey=" + key).toUpperCase();
				String result = ToolHttp.post(false, queryOrderUrl, paramStr, null);
				if(result!=null&&!result.equals("")){
					JSONObject resultJson = JSONObject.parseObject(result);
					JSONObject data = resultJson.getJSONObject("data");
					String status = data.getString("orderStatus");//充值状态
					if(status.equals("3")){//充值成功
						flag = 1;
					}else if(status.equals("1")){//充值中
						continue;
					}else{//充值失败
						flag = 0;
					}
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			log.error("深圳博亚流量查询Task出错"+e);
		}
	}
	
}
